<?php

namespace app\index\library\traits;

trait Backend
{

    /**
     * 查看
     */
    public function list(){

        $condition = [];

        $_condition = $this->_condition($condition);

        $whereOr = [];

        if(count($_condition) == 4){
            list($filer_arr, $op_arr, $filer_or_arr, $op_or_arr) = $_condition;
            $whereOr = $this->buildparams_where($filer_or_arr, $op_or_arr);
        }else{
            list($filer_arr, $op_arr) = $_condition;
        }

        $page_num = $this->request->get("page_num", 1);
        $page_size = $this->request->get("page_size", 20);

        list($where, $sort, $order, $offset, $limit) = $this->buildparams($filer_arr, $op_arr);

        $list = $this->model;

        if($this->sum_field){
            $list = $list->field($this->sumFieldChange($this->sum_field));
        }else{
            $total = $this->model
                ->where($where);
            if($whereOr){
                $total = $total->whereOr($whereOr);
            }
            $total = $total->order($sort, $order)
                ->count();
        }

        if($whereOr){
            $list = $list->whereOr($whereOr);
        }
        $list = $list
            ->where($where)
            ->order($sort, $order)
            ->limit($offset, $limit);
        if($this->relationSearchArr){
            $list = $list->with($this->relationSearchArr);
        }
        $list = $list->select();
        
        $this->listToArray($list);
        $list = $this->state_from_num_to_text($list);
        if($this->sum_field){
            $result = array("list" => $list);
        }else{
            $result = array("total" => $total, "page_total"=> ceil($total/$page_size), "page_num"=>ceil($page_num), "page_size"=>ceil($page_size), "list" => $list);
        }
        $this->success('success', $result);
        return true;
    }

    /**
     * 编辑
     */
    public function update($ids = NULL)
    {
        if(is_null($ids)){
            $ids = $this->request->param($this->primarykey??'id', '');
        }
        $row = $this->model->get($ids);
        if (!$row)
            $this->error('没找到记录');
        $params = $this->request->param();
        if ($params) {
            try {
                //是否采用模型验证
                if ($this->modelValidate) {
                    $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                    $row->validate($validate);
                }
                $result = $row->allowField(true)->save($params);
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error($row->getError());
                }
            } catch (\think\exception\PDOException $e) {
                $this->error($e->getMessage());
            } catch (\think\Exception $e) {
                $this->error($e->getMessage());
            }
        }
        $this->error('参数不能为空');
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        $pk = $this->model->getPk();
        $ids = $this->request->param($pk, '');
        if ($ids) {
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v) {
                $count += $v->delete();
            }
            if ($count) {
                $this->success();
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数不能为空');
    }

    /**
     * 添加
     */
    public function add()
    {
        $params = $this->request->param();
        if ($params) {
            try {
                //是否采用模型验证
                if ($this->modelValidate) {
                    $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                    $this->model->validate($validate);
                }
                $result = $this->model->allowField(true)->save($params);
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error($this->model->getError());
                }
            } catch (\think\exception\PDOException $e) {
                $this->error($e->getMessage());
            } catch (\think\Exception $e) {
                $this->error($e->getMessage());
            }
        }
        $this->error('参数不能为空');
    }

}
